home *** CD-ROM | disk | FTP | other *** search
/ PC Media 23 / PC MEDIA CD23.iso / share / prog / anubis / afd.h next >
Encoding:
C/C++ Source or Header  |  1995-08-05  |  3.7 KB  |  134 lines

  1. // AFD.H
  2. // (C) Anubis Software Agosto 1995.
  3. // Esta librería sirve para el uso de automatas finitos deterministas.
  4. #ifndef AFD_H
  5. #define AFD_H
  6.  
  7. // NOTAS
  8. // =====
  9.  
  10. // --------------------------------------+
  11. // Librerías Borland C++                 |
  12. // --------------------------------------+
  13. #include <stdlib.h>
  14.  
  15. // --------------------------------------+
  16. // Librerías Anubis Software             |
  17. // --------------------------------------+
  18. #include "mdefs.h"
  19.  
  20. // --------------------------------------+
  21. // Definición de constantes              |
  22. // --------------------------------------+
  23. #define GENERAL 255                             
  24.  
  25. // --------------------------------------+
  26. // Declaracion de prototipos             |
  27. // --------------------------------------+
  28.  
  29. typedef struct  {
  30.    void   *NodoFin;     // Es un apuntador a AFDNodo
  31.    void  *Siguiente;    // Es un apuntador a AFDArista
  32.    unsigned char Caracter;
  33.    void *Apuntador;
  34. } AFDArista;
  35.  
  36. typedef struct  {
  37.    boolean Terminal;
  38.    AFDArista *Aristas;
  39.    AFDArista *General;
  40.    void *Apuntador;
  41. } AFDNodo;
  42.  
  43. typedef struct {
  44.    AFDNodo *Inicio,
  45.        *Actual;
  46. } AFD;
  47.  
  48. // -----------------------------------------------+
  49. // Macros de la libreria                          |
  50. // -----------------------------------------------+
  51. #define AFDSetTerminal(x)   (*x).Terminal = TRUE
  52. #define AFDSetInicio(x,y)   (*x).Inicio  = y
  53. #define AFDResetea(x)       (*x).Actual  = (*x).Inicio
  54. #define AFDEsTerminal(x)    ((*x).Terminal)
  55. #define AFDFin(x)           ((*(*x).Actual).Terminal)
  56. // -----------------------------------------------+
  57. // Declaración e implementación de las funciones  | 
  58. // -----------------------------------------------+
  59.  
  60. AFDNodo *AFDAnula(AFD *automat)
  61. {
  62.     AFDNodo *nodo;
  63.     nodo= (AFDNodo *) malloc (sizeof(AFDNodo));
  64.         (*nodo).Aristas = NULL;
  65.     (*nodo).General = NULL;
  66.         (*nodo).Terminal = FALSE;
  67.     (*automat).Inicio = nodo;
  68.     (*automat).Actual = nodo;
  69.    return(nodo);
  70. }// end AFDAnula
  71.  
  72.  
  73. AFDNodo *AFDCrea(AFDNodo *nodobase, unsigned char acontecimiento, void *apuntador)
  74. {
  75.     AFDNodo *nodo;
  76.     AFDArista *arista;
  77.     nodo = (AFDNodo *) malloc (sizeof(AFDNodo));
  78.     (*nodo).Aristas = NULL;
  79.     (*nodo).General = NULL;
  80.     (*nodo).Apuntador = NULL;
  81.    (*nodo).Terminal = FALSE;
  82.     arista = (AFDArista *) malloc (sizeof (AFDArista));
  83.     if( acontecimiento != GENERAL)  {
  84.        (*arista).Siguiente = (*nodobase).Aristas;
  85.         (*nodobase).Aristas = arista;
  86.     }  else  {
  87.         (*arista).Siguiente = NULL;
  88.         (*nodobase).General = arista;
  89.     }// end if
  90.     (*arista).Caracter= acontecimiento;
  91.     (*arista).NodoFin = nodo;
  92.     (*arista).Apuntador=apuntador;
  93.     return(nodo);
  94. }// end AFDCrea
  95.  
  96.  
  97. AFDArista *AFDAnade(AFDNodo *nodobase, unsigned char acontecimiento, AFDNodo *nodofin)
  98. {
  99.     AFDArista *arista;
  100.     arista = (AFDArista *) malloc (sizeof (AFDArista));
  101.    (*arista).Caracter = acontecimiento;
  102.     (*arista).NodoFin = nodofin;
  103.    (*arista).Apuntador = NULL;
  104.     if ( acontecimiento != GENERAL)  {
  105.         (*arista).Siguiente = (*nodobase).Aristas;
  106.       (*nodobase).Aristas = arista;
  107.     }  else  {
  108.         (*arista).Siguiente = NULL;
  109.         (*nodobase).General = arista;
  110.     }// end if
  111.    return(arista);
  112. }// end AFDAnade
  113.     
  114.  
  115. AFDArista *AFDAcontecimiento(AFD *automat, char acont, AFDNodo **nodo)
  116. {
  117.     AFDArista *aaux;
  118.     aaux = (*((*automat).Actual)).Aristas;
  119.     while ( (aaux != NULL) && ( (*aaux).Caracter != acont))
  120.         aaux = (*aaux).Siguiente;
  121.     if ( aaux != NULL )  {
  122.         (*automat).Actual = (*aaux).NodoFin;
  123.         *nodo = (*aaux).NodoFin;
  124.         return(aaux);
  125.    }  else  {
  126.         aaux = (*((*automat).Actual)).General;
  127.         (*automat).Actual = (*aaux).NodoFin;
  128.         *nodo = (*aaux).NodoFin;
  129.         return(aaux);
  130.     }// end if
  131. }// end AFDAcontecimiento.
  132.  
  133. #endif
  134.